//
//  SystemSettingVC.m
//  hnGovernment
//
//  Created by 安仔 on 17/3/3.
//  Copyright © 2017年 安仔. All rights reserved.
//

#import "SystemSettingVC.h"
#import "AboutUsVC.h"
#import "ChangePswVC.h"
#import "DDcacheManager.h"
#import "DDbaseNetWork+Login.h"
#import "DDcacheManager.h"

@interface SystemSettingVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSArray *myTVDataArr;
@property (nonatomic, strong) UIButton *quitBtn;
@property (nonatomic, strong) UITableViewHeaderFooterView *footerView;
@end

@implementation SystemSettingVC
{
    BOOL flag;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalString(@"系统设置");
    self.view.backgroundColor = RGB(243, 243, 243);
    [self createSystemSettingView];
    [self setUpData];
}

//初始化数据
- (void)setUpData {
    NSDictionary *dic1 = @{@"image":@"修改密码",
                           @"title":NSLocalString(@"修改密码")};
    NSDictionary *dic2 = @{@"image":@"清除缓存",
                           @"title":NSLocalString(@"清除缓存")};
    NSDictionary *dic3 = @{@"image":@"关于我们",
                           @"title":NSLocalString(@"关于我们")};
    if ([cacheManager userToken]) {
        self.myTVDataArr = @[dic1, dic2, dic3];
        //self.quitBtn.hidden = NO;
        flag = YES;
    }else {
        self.myTVDataArr = @[dic2,dic3];
        //self.quitBtn.hidden = YES;
        flag = NO;
    }
}

- (void) quitLogin {
    
    [DDNetWorkManager userLoginOutWithToken:[cacheManager userToken] Controller:self success:^(id result, DDcacheType cacheType, DDresultType resultType) {
    } failure:^(NSError *error) {
    }];
    [cacheManager clearUserInfo];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- UITableView
- (void)createSystemSettingView {
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - 128*m6Scale) style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    self.myTableView.scrollEnabled = NO;
    [self.myTableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"footerView"];
    [self.view addSubview:self.myTableView];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.myTVDataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20*m6Scale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100 * m6Scale;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return self.myTableView.frame.size.height-(20+self.myTVDataArr.count*100)*m6Scale;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    self.footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"footerView"];
    self.quitBtn = [[UIButton alloc] init];
    [self.quitBtn setTitle:NSLocalString(@"退出登录") forState:0];
    [self.quitBtn setTitleColor:[UIColor whiteColor] forState:0];
    self.quitBtn.backgroundColor = APPColor;
    self.quitBtn.layer.cornerRadius = 10 * m6Scale;
    if (flag) {
        self.quitBtn.hidden = NO;
    } else {
        self.quitBtn.hidden = YES;
    }
    [self.quitBtn addTarget:self action:@selector(quitLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.footerView addSubview:self.quitBtn];
    [self.quitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.footerView.mas_centerX);
        make.bottom.mas_equalTo(self.footerView.mas_bottom).offset(-200*m6Scale);
        make.size.mas_equalTo(CGSizeMake(600*m6Scale, 80*m6Scale));
    }];
    return self.footerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellid"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellid"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    NSDictionary *dict = self.myTVDataArr[indexPath.row];
    cell.textLabel.text = dict[@"title"];
    cell.textLabel.textColor = [UIColor colorWithHexString:@"666666"];
    cell.imageView.image = [UIImage imageNamed:dict[@"image"]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dict = self.myTVDataArr[indexPath.row];
    NSString *name = dict[@"title"];
    if ([name isEqualToString:@"关于我们"]) {
        AboutUsVC *vc = [[AboutUsVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([name isEqualToString:@"修改密码"]) {
        ChangePswVC *vc = [[ChangePswVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已清理1M缓存" preferredStyle:UIAlertControllerStyleAlert];
        // 添加按钮
        __weak typeof(alert) weakAlert = alert;
        UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            NSLog(@"点击了确定按钮");
        }];
        [defaultAction setValue:APPColor forKey:@"_titleTextColor"];
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
